﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.Service.Model;
using System.Data;
using GtsServer.DAL;
using GtsServer.Model;
using GtsServer.Common;

namespace GtsServer.Service.Controller
{
    public class CaidanTaskController
    {
        public CaidanTaskController() { }

        TaskUserDAL taskUserDal = new TaskUserDAL();

        public CaidanTaskResponse GetCaidanTask(CaidanTaskRequest request)
        {
            CaidanTaskResponse response = new CaidanTaskResponse();
            response.task = new List<CaidanTaskManage>();

            DataSet ds = new DataSet();
            string userAccount = BaseController.Account(request.info);
            DateTime dt = DateTime.Now;

            if (string.IsNullOrEmpty(userAccount) || (dt.Hour == 23 && dt.Minute >= 55) || (dt.Hour == 0 && dt.Minute <= 5))
                return response;

            //获取用户的彩蛋任务
            ds = taskUserDal.GetTaskUserListByProcedure(userAccount, Convert.ToInt32(EnumTaskType.彩蛋任务));
            List<TaskUserShow> list = IListDataSet.DataSetToIList<TaskUserShow>(ds, 0).ToList();
            bool isSuccess = false;

            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    TaskUserShow item = list[i];
                    int index = item.SortNum == request.task.win_times.Count ? request.task.win_times.Count - 1 : item.SortNum;
                    if (item.TaskMouldConditionID.Equals(Convert.ToInt32(EnumTaskConditionMould.压中次)))
                        item.Score = request.task.win_times[index];
                    else if (item.TaskMouldConditionID.Equals(Convert.ToInt32(EnumTaskConditionMould.押注分)))
                        item.Score = request.task.earn_coin[index];
                    else if (item.TaskMouldConditionID.Equals(Convert.ToInt32(EnumTaskConditionMould.分累计)))
                        item.Score = request.task.earn_coin.Sum();
                    //改变用户任务完成状态
                    int value = Convert.ToInt32(item.ConditionValue);
                    if (item.State != Convert.ToInt32(EnumTaskUserState.已领取))
                        item.State = item.Score >= value ? Convert.ToInt32(EnumTaskUserState.完成待领取) : Convert.ToInt32(EnumTaskUserState.未完成);

                    int conditionvalue = Convert.ToInt32(item.ConditionValue);
                    if (item.Score >= conditionvalue)
                        item.Score = conditionvalue;
                }

                isSuccess = taskUserDal.UpdateSqlTran(list);
            }
            if (isSuccess)
            {
                foreach (var item in list)
                {
                    CaidanTaskManage model = new CaidanTaskManage()
                    {
                        id = item.TaskID,
                        type = item.ImageType,
                        state = item.State.Value,
                        egg = item.PrizeCount.ToString(),
                        tit = item.TaskName,
                        condition = item.TaskContent,
                        present = item.Score + "/" + item.ConditionValue
                    };
                    response.task.Add(model);
                }
            }
            return response;
        }
    }
}
